#include "SpaceFortress.h"

#include "Comodities.h"

BidData::BidData()
{
	size = 0;
	capacity = 0;
	buffer = NULL;

	entity = NULL;
	quantity = NULL;
	quantity_traded = NULL;
	price = NULL;
	clearing_price = NULL;
	storage_index = NULL;
}

BidData::~BidData()
{
	if (capacity) {
		delete[] buffer;
	}
	buffer = NULL;

	entity = NULL;
	quantity = NULL;
	quantity_traded = NULL;
	price = NULL;
	clearing_price = NULL;
	storage_index = NULL;
}

void BidData::Initialize(const uint32 set_capacity)
{
	capacity = set_capacity;

	const uint32 bytes = capacity * (sizeof(Entity) + 2 * sizeof(float) + 3 * sizeof(uint32));
	buffer = new unsigned char[bytes];

	entity = (Entity*)(buffer);
	quantity = (float*)(entity + set_capacity);
	quantity_traded = (float*)(quantity + set_capacity);
	price = (uint32*)(quantity_traded + set_capacity);
	clearing_price = (uint32*)(price + set_capacity);
	storage_index = (uint32*)(clearing_price + set_capacity);
}

PriceTracking::PriceTracking()
{
	index = 0;
	memset(day, 0, 15 * sizeof(uint32));
}

void PriceTracking::Insert(const uint32 price)
{
	day[index] = price;
	index = (index + 1) % 15;
}

const float PriceTracking::Average()
{
	float average = 0;
	for (uint32 i = 0; i < 15; i++) {
		average += day[i];
	}

	average /= 15.f;
	return average;
}